using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UI_BackPack : MonoBehaviour
{
    [SerializeField] private UI_InventorySlot[] _inventorySlots;
    [SerializeField] private UI_EquipmentSlot[] _equipmentSlots;
    [SerializeField] private UI_StatSlot[] _statSlots;
    private UI uI;

    public ItemDataSO _selectedEquipmentDataSO;

    private void Awake()
    {
        uI = GetComponentInParent<UI>();
    }

    public void SetupBackPackUI(List<ItemDataSO> itemDataSOList, Dictionary<int, ItemDataSO> equipmentDataSO, PlayerStatGroup playerStatGroup)
    {
        for (int i = 0; i < _inventorySlots.Length; i++)
        {
            if (i < itemDataSOList.Count)
                _inventorySlots[i].SetupInventorySlot(itemDataSOList[i]);
            else
                _inventorySlots[i].HiddleInventorySlot();
        }

        for (int i = 0; i < _equipmentSlots.Length; i++)
        {
            _equipmentSlots[i].HiddleEquipSlot();

            foreach (var equip in equipmentDataSO)
            {
                if (_equipmentSlots[i].GetCurrentSlotNum() == equip.Key)
                    _equipmentSlots[i].SetupEquipmentUI(equip.Value);
            }
        }

        for(int i = 0; i < _statSlots.Length; i++)
        {
            _statSlots[i].SetupStatSlot(playerStatGroup);
        }
    }

    public void SetEquipmentDataSO(ItemDataSO itemDataSO)
    {
        _selectedEquipmentDataSO = itemDataSO;
        for(int i = 0; i < _inventorySlots.Length; i++)
        {
            if (_inventorySlots[i].CurrentItemDataSO == itemDataSO)
                continue;
            _inventorySlots[i].ResetClickState();
        }
    }

    public void CloseBackPack()
    {
        _selectedEquipmentDataSO = null;
        gameObject.SetActive(false);
        uI.EnableUIInput(false);
    }
}
